package com.jane.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.jane.adapter.hostheadlinesadapter.HostHeadAdapter;
import com.jane.bean.HostHeadLinesBean;
import com.jane.bean.JsonData;
import com.jane.config.NetConfig;
import com.jane.gzzb.HostHeadLinesActivity;
import com.jane.gzzb.R;
import com.jane.utils.Utils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * create an instance of this fragment.
 */
public class HostHeadlinesFragment extends Fragment implements AbsListView.OnScrollListener, AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener {
    private ListView mListView;
    private HostHeadAdapter mAdapter;
    private SwipeRefreshLayout mLayout;
    private ArrayList<HostHeadLinesBean.DataListBean> mData;
    private View view;
    private boolean isLoad;
    private int mPager=0;
    private String path;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_host_headlines, container, false);
        initView();
        initData();
        setData();
        setListener();
        return view;
    }
    private void initView() {
        mLayout= (SwipeRefreshLayout) view.findViewById(R.id.host_fragment_srl);
        mLayout.setColorSchemeColors(Color.parseColor("#00FF00"),Color.parseColor("#FF671E"),Color.parseColor("#0000FF"));
        mListView= (ListView) view.findViewById(R.id.host_fragment_listView);
    }
    private void initData() {
        //初始化数据及适配器
        mData=new ArrayList<>();
        mAdapter=new HostHeadAdapter(getActivity(),mData);
        //路径
        path = NetConfig.HOSTHEADLINES_PATH;
        String jsonString=Utils.getJsonString(path);
        if (!TextUtils.isEmpty(jsonString)){
            parseData(jsonString);//解析数据
        }else {
            //判断是否有网络
            if (!Utils.isNetwork(getActivity())){
                return;
            }
            netLoadData();//网络加载数据
        }

    }

    private void netLoadData() {
        //网络加载数据
        x.http().get(new RequestParams(path), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (mPager==0){//保存一份数据
                    JsonData jsonData=new JsonData();
                    jsonData.setPath(path);
                    jsonData.setJsonString(result);
                    Utils.saveJsonString(jsonData);
                   /* mData.clear();
                    HostHeadLinesBean bean=new Gson().fromJson(result,HostHeadLinesBean.class);
                    mData.addAll(bean.getData_list());
                    mAdapter.notifyDataSetChanged();*/
                }
                if (!TextUtils.isEmpty(result)){
                    parseData(result);
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                mLayout.setRefreshing(false);
            }

            @Override
            public void onCancelled(CancelledException cex) {
                mLayout.setRefreshing(false);
            }
            @Override
            public void onFinished() {
                mAdapter.notifyDataSetChanged();
                mLayout.setRefreshing(false);
            }
        });
    }

    private void parseData(String jsonString) {
        mData.clear();
        HostHeadLinesBean bean=new Gson().fromJson(jsonString,HostHeadLinesBean.class);
        mData.addAll(bean.getData_list());
        mAdapter.notifyDataSetChanged();
    }


    private void setData() {
        mListView.setAdapter(mAdapter);
    }

    private void setListener() {
        mLayout.setOnRefreshListener(this);
        mListView.setOnScrollListener(this);
        mListView.setOnItemClickListener(this);
    }


    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {
        if (isLoad==true && i==SCROLL_STATE_IDLE){
            mPager++;
            initData();
        }
    }

    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {
        if (i+i1==i2){
            isLoad=true;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent=new Intent(getActivity(), HostHeadLinesActivity.class);
        intent.putExtra("url",mData.get(i).getDetail_url());
        startActivity(intent);
    }

    @Override
    public void onRefresh() {
        initData();
    }
}
